package cn.kgc.musicserver.controller;

import cn.kgc.musicserver.domain.dto.Result;
import cn.kgc.musicserver.domain.dto.ResultUtil;
import cn.kgc.musicserver.domain.entity.Comment;
import cn.kgc.musicserver.service.ICommentService;
import cn.kgc.musicserver.service.ISongService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/comment")
public class CommentController {
    private final ICommentService commentService;

    @Autowired
    private ISongService songService;

    /**
     * 分页获取评论
     * @param pageNo
     * @param size
     * @param search
     * @return
     */
    @GetMapping("/page")
    public Result getCommentsPage(
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "size", defaultValue = "5") int size,
            @RequestParam(value = "search", required = false) String search) {

        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        if (search != null && !search.isEmpty()) {
            wrapper.like("content", search);
        }
        wrapper.orderByAsc("id");

        Page<Comment> page = new Page<>(pageNo, size);
        IPage<Comment> result = commentService.page(page, wrapper);

        return ResultUtil.returnDataSuccess(result);
    }


    @GetMapping("/like")
    public Result likeComment(@RequestParam Integer id) {
        if (id == null) {
            return ResultUtil.returnFail("评论ID不能为空");
        }
        Comment comment = commentService.getById(id);
        if (comment == null) {
            return ResultUtil.returnFail("评论不存在");
        }
        comment.setUp(comment.getUp() + 1);
        boolean success = commentService.updateById(comment);
        if (!success) {
            return ResultUtil.returnFail("点赞失败");
        }
        return ResultUtil.returnSuccess("点赞成功");
    }

    @GetMapping("/delete")
    public Result deleteComment(@RequestParam Integer id) {
        if (id == null) {
            return ResultUtil.returnFail("评论ID不能为空");
        }
        boolean success = commentService.removeById(id);
        if (!success) {
            return ResultUtil.returnFail("删除失败");
        }
        return ResultUtil.returnSuccess("删除成功");
    }
    @GetMapping("/pageByLikes")
    public Result getCommentsPageByLikes(
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "size", defaultValue = "5") int size,
            @RequestParam(value = "search", required = false) String search) {

        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        if (search != null && !search.isEmpty()) {
            wrapper.like("content", search);
        }
        // 按点赞数降序排序
        wrapper.orderByDesc("up");

        Page<Comment> page = new Page<>(pageNo, size);
        IPage<Comment> result = commentService.page(page, wrapper);

        return ResultUtil.returnDataSuccess(result);
    }
}